<?php

namespace app\comm\service;
use app\mall\model\Goods;
use app\comm\model\GoodsComment;
use app\comm\model\GoodsMaterial;
use app\comm\model\GoodsYongliao;
use app\comm\model\GoodsSpec;
use app\comm\model\GoodsSpecName;
use app\comm\model\StoreGoodsStock;
use app\comm\model\GoodsSpecDetail;
use think\Db;
use app\comm\service\UserService;
use ServS\DbShowcollo;
use ServS\Times;
use ServS\StringToo;
/**
 * 商品服务层
 * 
 */
class GoodsService{

	public function test(){
		DbShowcollo::refresh(new GoodsComment,'id',1,['uid'=>100]);
	}
	/**
	 * 按照id获取，good
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function getgoodbyid($id){
		$good = Goods::get($id);
		
		if(!$good){
			return null;
		}
		return $good->toArray();
	}

	/**
	 * 上商品详情，关联查询
	 */
	public function getgoodsallbyid($id){
		$goods = $this->getgoodbyid($id);
		
		$goods['commt']= $this->getpinglun($id);
		return $goods;
	}
/****************************评论***************************************/
	/**
	 * 评论商品
	 */
	public function addgoodscomm($array){
		$goodscomment = new GoodsComment();
		$goodscomment ->goods_id = $array['goods_id'];
		$goodscomment ->content = $array['content'];
		$goodscomment ->is_show = $array['is_show'];
		$goodscomment ->uid = $array['uid'];
		$goodscomment ->img = $array['img'];
		$goodscomment ->order_id = $array['order_id'];
		$goodscomment ->order_code = $array['order_code'];
		$goodscomment ->deliver_rank = $array['deliver_rank'];
		$goodscomment ->goods_rank = $array['goods_rank'];
		$goodscomment ->service_rank = $array['service_rank'];
		$goodscomment ->is_anonymous = $array['is_anonymous'];
		$goodscomment ->addtime = time();
		$good = $goodscomment ->save();
		if($good){
			return 1;
		}
		return 0;
	}

	/**
	 * 获取商品下的所有评论
	 * @param  [type] $goodid [description]
	 * @return [type]         [description]
	 */
	public function getpinglun($goodid){
		$userservice = new  UserService;
		$goods = GoodsComment::where('goods_id',$goodid)->select();
		$psh = [];
		foreach ($goods as $key => $value) {
			$val=$value->toArray();
			if(!$val['is_anonymous']){
				$val['shifonimin'] ='匿名';	
			}else{
				if($user=$userservice->getuserinfobyuid($val['uid'])){
					$val['shifonimin']	=$user['nickname'];
				}else{
					$val['shifonimin']	='李白';
				}
					
			}
			$val['addtime']=date('Y-m-d:H:i',$val['addtime']);
			array_push($psh,$val );
				

		}
		return $psh;
	}

	/**
	 * 
	 */
	/*****************************配方编辑*********************************/
	/**
	 * 获取商品配方的列表
	 */
	public function getpefangfuanli($rows,$urlcode,$get=''){
        cookie('rows', $rows >= 10 ? $rows : 20);
        if($get!==''){
        	$goods = Goods::where('name','like','%'.$get.'%' )->paginate($rows, false, ['query' =>$urlcode]);
        }else{
        	$goods = Goods::where('1=1')->paginate($rows, false, ['query' =>$urlcode]);
        }
		foreach ($goods as $key => &$value) {
			$value->martail = $this->getgoodsyouliaoby($value->id);
		}
		return $goods;
	}	


	/**
	 * 查询配方  一个配方可以由几种
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function getgoodsyouliaoby($id){
		$list = GoodsYongliao::where('goods_id',$id)->select();
		$psh = [];
		foreach ($list as $key => $value) {
			$marter     = $value->toArray();
			$goodsmaterial  = $this ->getgoodsmaterialby($marter['material_id']);
			if(!$goodsmaterial)continue;
			foreach ($goodsmaterial as $key1 => $value1){
				if($key1=='id'){
					$marter['marid'] =$value1;
					continue;
				}
				if($key1=='remark'){
					$marter['marremark'] =$value1;
					continue;
				}
				if($key1=='name'){
					$marter['name'] =$value1;
					continue;
				}	
			}
			array_push($psh, $marter);
		}
		return $psh;
	}

	/**
	 * 配方增加
	 */
	public function addrecipe($data){
		$GoodsYongliao = new GoodsYongliao;
		DbShowcollo::add($GoodsYongliao,$data);
		return 1;
	}

	/**
	 * 配方编辑
	 */
	public function editor($id,$data){
		$GoodsYongliao = new GoodsYongliao;
		DbShowcollo::refresh($GoodsYongliao,'id',$id,$data);
		return 1;
	}

	/**
	 * 删除配方
	 */
	public function delerecipe($id){
		$GoodsYongliao = new GoodsYongliao;
		DbShowcollo::dele($GoodsYongliao,$id);
		return 1;
	}

	/**
	 * 查询原材料
	 */
	public function getgoodsmaterialby($id){
		$goodsmaterial = GoodsMaterial::get($id);
		if(!empty($goodsmaterial))return $goodsmaterial->toArray();
		return null;
		
	}

	/**
	 * 原材料查询
	 */
	public function getgoodsmaterial(){
		$list = GoodsMaterial::all(['is_zigou'=>0]);
		$psh = [];
		foreach ($list as $key => $value) {
			array_push($psh, $value->toArray());
		}
		return $psh;
	}

/******************************商品评论******************/
	/**
	 * 商品评论列表
	 * @return [type] [description]
	 */
	public function litbioa($urlcode,$get=''){
	  $list = 	GoodsComment::where('content','like','%'.$get.'%' )->paginate(10, false, ['query' =>$urlcode]);
	  $userservice = new UserService;
	  foreach ($list as $key => &$value) {
	  		$value->shifouxianshi  = $value->is_show==1?"是":"不显示";
	  		$value->shifouxianshicn  = $value->is_show==1?"不显示":"显示";
	  		$value->goodsname   =   $this->getgoodbyid($value->goods_id)['name'];
	  		$value->addtime   =   date('Y-m-d H:i',$value->addtime);
	  		if($value->uid){
	  			$value->nickname  = $userservice->getuserinfobyuid($value->uid)['nickname'];	
	  		}else{
	  			$value->nickname  ='匿名';	
	  		}	
	  }
	  return $list;
	}

	/**
	 * 不让显示 ishowsLook
	 */
	public function ishowsLook($id){
		$commit = $this->getcommintbyid($id);
		$comit  =  new 	GoodsComment;
		if($commit['is_show']){
			$data=['is_show'=>'0'];
			$se =  DbShowcollo::refresh($comit,'id',$id,$data);
			return $se;
		}else{
			$data=['is_show'=>'1'];
			return DbShowcollo::refresh($comit,'id',$id,$data);
		}
	}

	/**
	 * 获取一条评论
	 * @return [type] [description]
	 */
	public function getcommintbyid($id){
		return GoodsComment::get($id)->toArray();
	}
/**********************************************************************/
	/**
	 * 获取商品列表
	 * @return [type] [description]
	 */
	public function getgoodslistall(){
		$list = Goods::where('1=1')->select();
		return $list;
	}


/************************************************************************/
	
	/**
	 * 获取商品属性值
	 */
	public function specbyid($id){
		return Db::table('ys_goods_spec_detail')->where('id',$id)->find();
	}


	/**
	 * 商品规格查询
	 */
	public  function getgoodspec($goodid){
		$proty = $this -> proty($goodid);
		foreach ($proty as $key => &$value) {
			$map['spec_id']  = $value['spec_id'];
			$map['goods_id'] = $value['goods_id'];
			$li = GoodsSpec::where('1=1')->where($map)->select();
			$psu = [];
			foreach ($li as $ke => $val) {
				$goods = $val->toArray();
		        $goods['spacname'] = $this->getpacename($goods['spec_id'])['name'];
		        array_push($psu,$goods);
			}
			$value['content'] = $psu;
		}
		return $proty;
	}

	/**
	 * 获取商品的规格id之后获取全部的信息
	 */
	public function getspacallbyid($id){

		if($id =="-1")return null;
		$goodsspec = GoodsSpecDetail::get($id)->toArray();
		$ids  = $goodsspec['specs'];
		$goodsid   = explode(',',$ids);
		$map['id'] = ['in',$ids];
		$good =  GoodsSpec::where($map)->select();
		$psuh = [];
		foreach ($good as $key => $value) {
			$val = $value->toArray();
			$val['name'] = $this->getpacename($val['spec_id'])['name'];
			array_push($psuh, $val);
		}
		$pr['proty'] = $psuh;
		$pr['pric'] = $goodsspec;
		return $pr;
	}

	/**
	 * 单条查询 属性
	 */
	public function getpacename($id){
		return GoodsSpecName::get($id)->toArray();
	}

	/**
	 * 属性筛选
	 */
	public function  getgoodspiec($data){
		$map['specs']=['like','%'.$data['param'].'%'];
		$list = GoodsSpecDetail::where('1=1')->where($map)->select();
		$push=[];
		foreach ($list as $key => $value) {
			$push[] = $value->toArray();
		}
		return $push;
	}


	/**
	 * 生成订单
	 */
	public function contenorder($uid='10000'){
		return Times::getdate().time().rand(1000,9999).$uid;
	}

	/**
	 * 商品属性查询
	 */
	public function proty($goodid){
		$list = GoodsSpec::where('1=1')->where('goods_id',$goodid)->group('spec_id')->select();
		$push = [];
		foreach ($list as $key => $value) {
			$val = $value->toArray();
			$map['spec_id']   = $val['spec_id'];
			$map['goods_id']  = $goodid;
			$map['spec_name'] = $this->getpacename($val['spec_id'])['name'];
			array_push($push, $map);
		}
		return $push;
	}

	/**
	 * 商品库存计算
	 */
	public function goodstor($goodsid,$store,$spec='-1'){
		$map["goods_id"] 	  = $goodsid;
		$map["store_id"]	  = $store;
		$map["goods_spec_id"] = $spec;
		$map = StoreGoodsStock::where($map)->select();
		return $map;
	}

	/**
	 * 商品评论
	 */
	public function goodscomment($arry){
		$array['goods_id'] =  $arry['id'];
		$array['content']  =   $arry['msg'];
		$array['is_show']  =	1;
		$array['uid']  	   =	$arry['uid'];
		$array['store_id'] =	$arry['store_id'];
		$array['order_id'] =	$arry['order_id'];
		$array['addtime']     =	time();
		Db::table('ys_goods_comment')->insert($array);
	}

}

